package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.PigDAO;
import cn.edu.scau.cmi.hombio.dao.PigTypeDAO;
import cn.edu.scau.cmi.hombio.dao.SaleTaskQualityDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPigstyDAO;

import cn.edu.scau.cmi.hombio.domain.Pig;
import cn.edu.scau.cmi.hombio.domain.PigType;
import cn.edu.scau.cmi.hombio.domain.SaleTaskQuality;
import cn.edu.scau.cmi.hombio.domain.TaskPigsty;

import cn.edu.scau.cmi.hombio.service.PigTypeService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for PigType entities
 * 
 */

@Controller("PigTypeRestController")

public class PigTypeRestController {

	/**
	 * DAO injected by Spring that manages Pig entities
	 * 
	 */
	@Autowired
	private PigDAO pigDAO;

	/**
	 * DAO injected by Spring that manages PigType entities
	 * 
	 */
	@Autowired
	private PigTypeDAO pigTypeDAO;

	/**
	 * DAO injected by Spring that manages SaleTaskQuality entities
	 * 
	 */
	@Autowired
	private SaleTaskQualityDAO saleTaskQualityDAO;

	/**
	 * DAO injected by Spring that manages TaskPigsty entities
	 * 
	 */
	@Autowired
	private TaskPigstyDAO taskPigstyDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for PigType entities
	 * 
	 */
	@Autowired
	private PigTypeService pigTypeService;

	/**
	 * Create a new TaskPigsty entity
	 * 
	 */
	@RequestMapping(value = "/PigType/{pigtype_id}/taskPigsties", method = RequestMethod.POST)
	@ResponseBody
	public TaskPigsty newPigTypeTaskPigsties(@PathVariable Integer pigtype_id, @RequestBody TaskPigsty taskpigsty) {
		pigTypeService.savePigTypeTaskPigsties(pigtype_id, taskpigsty);
		return taskPigstyDAO.findTaskPigstyByPrimaryKey(taskpigsty.getId());
	}

	/**
	* Show all PigType entities
	* 
	*/
	@RequestMapping(value = "/PigType", method = RequestMethod.GET)
	@ResponseBody
	public List<PigType> listPigTypes() {
		return new java.util.ArrayList<PigType>(pigTypeService.loadPigTypes());
	}

	/**
	* Save an existing Pig entity
	* 
	*/
	@RequestMapping(value = "/PigType/{pigtype_id}/pigs", method = RequestMethod.PUT)
	@ResponseBody
	public Pig savePigTypePigs(@PathVariable Integer pigtype_id, @RequestBody Pig pigs) {
		pigTypeService.savePigTypePigs(pigtype_id, pigs);
		return pigDAO.findPigByPrimaryKey(pigs.getId());
	}

	/**
	* Select an existing PigType entity
	* 
	*/
	@RequestMapping(value = "/PigType/{pigtype_id}", method = RequestMethod.GET)
	@ResponseBody
	public PigType loadPigType(@PathVariable Integer pigtype_id) {
		return pigTypeDAO.findPigTypeByPrimaryKey(pigtype_id);
	}

	/**
	* View an existing TaskPigsty entity
	* 
	*/
	@RequestMapping(value = "/PigType/{pigtype_id}/taskPigsties/{taskpigsty_id}", method = RequestMethod.GET)
	@ResponseBody
	public TaskPigsty loadPigTypeTaskPigsties(@PathVariable Integer pigtype_id, @PathVariable Integer related_taskpigsties_id) {
		TaskPigsty taskpigsty = taskPigstyDAO.findTaskPigstyByPrimaryKey(related_taskpigsties_id, -1, -1);

		return taskpigsty;
	}

	/**
	* Show all TaskPigsty entities by PigType
	* 
	*/
	@RequestMapping(value = "/PigType/{pigtype_id}/taskPigsties", method = RequestMethod.GET)
	@ResponseBody
	public List<TaskPigsty> getPigTypeTaskPigsties(@PathVariable Integer pigtype_id) {
		return new java.util.ArrayList<TaskPigsty>(pigTypeDAO.findPigTypeByPrimaryKey(pigtype_id).getTaskPigsties());
	}

	/**
	* Save an existing TaskPigsty entity
	* 
	*/
	@RequestMapping(value = "/PigType/{pigtype_id}/taskPigsties", method = RequestMethod.PUT)
	@ResponseBody
	public TaskPigsty savePigTypeTaskPigsties(@PathVariable Integer pigtype_id, @RequestBody TaskPigsty taskpigsties) {
		pigTypeService.savePigTypeTaskPigsties(pigtype_id, taskpigsties);
		return taskPigstyDAO.findTaskPigstyByPrimaryKey(taskpigsties.getId());
	}

	/**
	* Delete an existing Pig entity
	* 
	*/
	@RequestMapping(value = "/PigType/{pigtype_id}/pigs/{pig_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigTypePigs(@PathVariable Integer pigtype_id, @PathVariable Integer related_pigs_id) {
		pigTypeService.deletePigTypePigs(pigtype_id, related_pigs_id);
	}

	/**
	* Create a new SaleTaskQuality entity
	* 
	*/
	@RequestMapping(value = "/PigType/{pigtype_id}/saleTaskQualities", method = RequestMethod.POST)
	@ResponseBody
	public SaleTaskQuality newPigTypeSaleTaskQualities(@PathVariable Integer pigtype_id, @RequestBody SaleTaskQuality saletaskquality) {
		pigTypeService.savePigTypeSaleTaskQualities(pigtype_id, saletaskquality);
		return saleTaskQualityDAO.findSaleTaskQualityByPrimaryKey(saletaskquality.getId());
	}

	/**
	* Delete an existing PigType entity
	* 
	*/
	@RequestMapping(value = "/PigType/{pigtype_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigType(@PathVariable Integer pigtype_id) {
		PigType pigtype = pigTypeDAO.findPigTypeByPrimaryKey(pigtype_id);
		pigTypeService.deletePigType(pigtype);
	}

	/**
	* Show all SaleTaskQuality entities by PigType
	* 
	*/
	@RequestMapping(value = "/PigType/{pigtype_id}/saleTaskQualities", method = RequestMethod.GET)
	@ResponseBody
	public List<SaleTaskQuality> getPigTypeSaleTaskQualities(@PathVariable Integer pigtype_id) {
		return new java.util.ArrayList<SaleTaskQuality>(pigTypeDAO.findPigTypeByPrimaryKey(pigtype_id).getSaleTaskQualities());
	}

	/**
	* Delete an existing TaskPigsty entity
	* 
	*/
	@RequestMapping(value = "/PigType/{pigtype_id}/taskPigsties/{taskpigsty_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigTypeTaskPigsties(@PathVariable Integer pigtype_id, @PathVariable Integer related_taskpigsties_id) {
		pigTypeService.deletePigTypeTaskPigsties(pigtype_id, related_taskpigsties_id);
	}

	/**
	* Save an existing PigType entity
	* 
	*/
	@RequestMapping(value = "/PigType", method = RequestMethod.PUT)
	@ResponseBody
	public PigType savePigType(@RequestBody PigType pigtype) {
		pigTypeService.savePigType(pigtype);
		return pigTypeDAO.findPigTypeByPrimaryKey(pigtype.getId());
	}

	/**
	* View an existing SaleTaskQuality entity
	* 
	*/
	@RequestMapping(value = "/PigType/{pigtype_id}/saleTaskQualities/{saletaskquality_id}", method = RequestMethod.GET)
	@ResponseBody
	public SaleTaskQuality loadPigTypeSaleTaskQualities(@PathVariable Integer pigtype_id, @PathVariable Integer related_saletaskqualities_id) {
		SaleTaskQuality saletaskquality = saleTaskQualityDAO.findSaleTaskQualityByPrimaryKey(related_saletaskqualities_id, -1, -1);

		return saletaskquality;
	}

	/**
	* Delete an existing SaleTaskQuality entity
	* 
	*/
	@RequestMapping(value = "/PigType/{pigtype_id}/saleTaskQualities/{saletaskquality_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigTypeSaleTaskQualities(@PathVariable Integer pigtype_id, @PathVariable Integer related_saletaskqualities_id) {
		pigTypeService.deletePigTypeSaleTaskQualities(pigtype_id, related_saletaskqualities_id);
	}

	/**
	* Create a new Pig entity
	* 
	*/
	@RequestMapping(value = "/PigType/{pigtype_id}/pigs", method = RequestMethod.POST)
	@ResponseBody
	public Pig newPigTypePigs(@PathVariable Integer pigtype_id, @RequestBody Pig pig) {
		pigTypeService.savePigTypePigs(pigtype_id, pig);
		return pigDAO.findPigByPrimaryKey(pig.getId());
	}

	/**
	* View an existing Pig entity
	* 
	*/
	@RequestMapping(value = "/PigType/{pigtype_id}/pigs/{pig_id}", method = RequestMethod.GET)
	@ResponseBody
	public Pig loadPigTypePigs(@PathVariable Integer pigtype_id, @PathVariable Integer related_pigs_id) {
		Pig pig = pigDAO.findPigByPrimaryKey(related_pigs_id, -1, -1);

		return pig;
	}

	/**
	* Show all Pig entities by PigType
	* 
	*/
	@RequestMapping(value = "/PigType/{pigtype_id}/pigs", method = RequestMethod.GET)
	@ResponseBody
	public List<Pig> getPigTypePigs(@PathVariable Integer pigtype_id) {
		return new java.util.ArrayList<Pig>(pigTypeDAO.findPigTypeByPrimaryKey(pigtype_id).getPigs());
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Save an existing SaleTaskQuality entity
	* 
	*/
	@RequestMapping(value = "/PigType/{pigtype_id}/saleTaskQualities", method = RequestMethod.PUT)
	@ResponseBody
	public SaleTaskQuality savePigTypeSaleTaskQualities(@PathVariable Integer pigtype_id, @RequestBody SaleTaskQuality saletaskqualities) {
		pigTypeService.savePigTypeSaleTaskQualities(pigtype_id, saletaskqualities);
		return saleTaskQualityDAO.findSaleTaskQualityByPrimaryKey(saletaskqualities.getId());
	}

	/**
	* Create a new PigType entity
	* 
	*/
	@RequestMapping(value = "/PigType", method = RequestMethod.POST)
	@ResponseBody
	public PigType newPigType(@RequestBody PigType pigtype) {
		pigTypeService.savePigType(pigtype);
		return pigTypeDAO.findPigTypeByPrimaryKey(pigtype.getId());
	}
}